package cc.geguo.action;


import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.util.ValueStack;

import java.util.List;

import cc.geguo.entity.Employee;
import cc.geguo.service.IEmployeeService;
import cc.geguo.service.impl.EmployeeService;

/**
 *职员
 */
public class EmployeeAction extends ActionSupport implements ModelDriven<Employee> {
	
	private Employee employee = new Employee();
	public Employee getEmployee() {
		return employee;
	}
	public void setEmployee(Employee employee) {
		this.employee = employee;
	}
	@Override
	public Employee getModel() {
		return employee;
	}
	

	
	private IEmployeeService employeeService = new EmployeeService();
	
	/**
	 * 保存
	 */
	public String save() {
		try {
			employeeService.save(employee);
			return list();
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}
	
	/**
	 * 显示列表
	 */
	public String list() {
		try {
			List<Employee> listEmp = employeeService.getAll();
			ActionContext.getContext().getContextMap().put("listEmp", listEmp);
			return "list";
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}


	/**
	 * @return 数据回显
	 */
	public String viewUpdate() {
		try {
			int id = employee.getId();
			// 3.2 service��ѯ
			Employee emp = employeeService.findById(id);
			ValueStack vs = ActionContext.getContext().getValueStack();
			vs.pop();
			vs.push(emp);
			
			return "update";
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}


	/**
	 * @return 更新
	 */
	public String update() {
		try {
			employeeService.update(employee);
			return list();
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}
}
